package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.GoodsCategoryListAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.GoodsListAdapter;
import com.diudiutechnology.www.yadiudiu.util.view.MyListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.ShopOrderActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import se.emilsjolander.stickylistheaders.ExpandableStickyListHeadersListView;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * Created by Administrator on 2017/8/21.
 * 弃用
 */

public class GoodsActivity extends Activity implements View.OnClickListener, StickyListHeadersListView.OnHeaderClickListener,
        StickyListHeadersListView.OnStickyHeaderChangedListener,
        StickyListHeadersListView.OnStickyHeaderOffsetChangedListener,GoodsListAdapter.OnShopCartGoodsChangeListener {
    Context context;
    @Bind(R.id.goodsCateGoryList)//商品列表
            MyListView goodsCateGoryList;
    @Bind(R.id.goodsList)//商品类型
            ExpandableStickyListHeadersListView goodsList;
    @Bind(R.id.shopCartNum)//加入商品的数量
            TextView shopCartNum;
    @Bind(R.id.contact)//提交订单
            TextView contact;

    private GoodsListAdapter goodsAdapter;//商品列表适配器
    private GoodsCategoryListAdapter goodsCategoryListAdapter;//商品类型列表适配器
    //商品类别列表
    private List<BuyACarFragmentModel> goodscatrgoryEntities;
    //商品列表
    private List<BuyACarFragmentModel> goodsitemEntities;
    //上一个标题的小标
    private int lastTitlePoi;
    private MyOnGoodsScrollListener myOnGoodsScrollListener;
    //存储含有标题的第一个含有商品类别名称的条目的下表
    private List<Integer> titlePois = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        contact.setOnClickListener(this);
        goodsCateGoryList.setDealTouchEvent(true);
        goodsList.setOnHeaderClickListener(this);
        goodsList.setOnStickyHeaderChangedListener(this);
        goodsList.setOnStickyHeaderOffsetChangedListener(this);
        goodsList.setDrawingListUnderStickyHeader(true);
        goodsList.setAreHeadersSticky(true);
        goodsCateGoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("titlePois"+position);
                goodsList.setSelection(titlePois.get(position));
            }
        });
        fillGoodsList();
    }
    /**
     * 填充商品列表
     */
    private void fillGoodsList() {
        goodscatrgoryEntities=new ArrayList<>();
        BuyACarFragmentModel model=new BuyACarFragmentModel();
        model.setName("洗衣");
        goodscatrgoryEntities.add(model);

        model=new BuyACarFragmentModel();
        model.setName("洗鞋");

        goodscatrgoryEntities.add(model);
        model=new BuyACarFragmentModel();
        model.setName("精洗");
        goodscatrgoryEntities.add(model);

        goodsitemEntities=new ArrayList<>();
        BuyACarFragmentModel models=new BuyACarFragmentModel();
        models.setName("羽绒服");
        models.setCategoryPosition(0);
        titlePois.add(0);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("羽绒服");
        models.setCategoryPosition(0);
        goodsitemEntities.add(models);

        models=new BuyACarFragmentModel();
        models.setName("羽绒服");
        models.setCategoryPosition(0);
        goodsitemEntities.add(models);

        models=new BuyACarFragmentModel();
        models.setName("反绒皮休闲鞋");
        titlePois.add(3);
        models.setCategoryPosition(1);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("反绒皮休闲鞋");

        models.setCategoryPosition(1);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("反绒皮休闲鞋");

        models.setCategoryPosition(1);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("反绒皮休闲鞋");

        models.setCategoryPosition(1);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("反绒皮休闲鞋");

        models.setCategoryPosition(1);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("反绒皮休闲鞋");

        models.setCategoryPosition(1);
        goodsitemEntities.add(models);
        models=new BuyACarFragmentModel();
        models.setName("皮草精洗");
        titlePois.add(9);
        models.setCategoryPosition(2);
        goodsitemEntities.add(models);
      //  goodsAdapter = new GoodsListAdapter(goodsitemEntities, context, goodscatrgoryEntities);
        goodsAdapter.setmActivity(this);
      goodsAdapter.setShopCart(shopCartNum);
        goodsList.setAdapter(goodsAdapter);
        goodsAdapter.setOnShopCartGoodsChangeListener(this);
     //   goodsCategoryListAdapter = new GoodsCategoryListAdapter(goodscatrgoryEntities, context);
        goodsCateGoryList.setAdapter(goodsCategoryListAdapter);
        myOnGoodsScrollListener = new MyOnGoodsScrollListener();
        goodsList.setOnScrollListener(myOnGoodsScrollListener);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.contact:
                startActivity(new Intent(context,ShopOrderActivity.class));
                break;
        }

    }

    @Override
    public void onHeaderClick(StickyListHeadersListView l, View header, int itemPosition, long headerId, boolean currentlySticky) {

    }

    @Override
    public void onStickyHeaderChanged(StickyListHeadersListView l, View header, int itemPosition, long headerId) {

    }

    @Override
    public void onStickyHeaderOffsetChanged(StickyListHeadersListView l, View header, int offset) {

    }

    @Override
    public void onNumChange() {

    }

    /**
     * 处理滑动 是两个ListView联动
     */
    private class MyOnGoodsScrollListener implements AbsListView.OnScrollListener {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (!(lastTitlePoi == goodsitemEntities
                    .get(firstVisibleItem)
                    .getCategoryPosition())) {
                lastTitlePoi = goodsitemEntities
                        .get(firstVisibleItem)
                        .getCategoryPosition();
                goodsCategoryListAdapter.setCheckID(goodsitemEntities
                        .get(firstVisibleItem)
                        .getCategoryPosition());
                goodsCategoryListAdapter.notifyDataSetChanged();
            }
        }
    }
}
